﻿///<reference path="../jquery-1.4.4.min.js" />
///<reference path="../kooboo.mvcvalidation.js" />


$.ajaxSetup({
    'beforeSend': function (xhr) {
       // xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    }
});

$.fn.ajaxGet = function (options, data, action) {
    if (typeof tinyMCE != 'undefined') {
        //update html editor
        tinyMCE.triggerSave();
    }

    var element = $(this);
    

    var defaultOptions = {
        error: function (response) {
            showError(response, options ? options.box : undefined);
        },
        message: function (response) {
            showMessage(response, options ? options.box : undefined);
        },
        timeout: function () {
            clearTimeout(timeoutId);
            $(".loading").hide();
        }
    };

    var settings = $.extend(defaultOptions, options);


    var timeoutId = window.setTimeout(function () { $(".loading").show(); }, 150);

    var fn = function (response, status, xhr) {
        clearTimeout(timeoutId);
        $(".loading").hide();

        if (xhr.status == 278) {
            var win = window.parent || window;
            win.location.href = xhr.getResponseHeader('Location');
            return;
        }
        if (typeof response.Error != 'undefined') {
            settings.error(response.Error);
        } else if (typeof response.Message != 'undefined') {
            settings.message(response.Message);
        } else if (typeof settings.success != 'undefined') {
            settings.success(response);
        }

        $(".loading").hide();
    };



    if (element.is("a")) {
        action = action || element.attr("href");
        data = data || "";
    }
    else {
        action = action || element.attr("action");
        data = data || element.serialize();
    }


    $.ajax({
        type: 'GET',
        url: action,
        data: data,
        success: fn,
        timeout: settings.timeout,
        dataType: "json"
    });
};

$.fn.ajaxPost = function (options, data, action) {

    if (typeof tinyMCE != 'undefined') {
        //update html editor
        tinyMCE.triggerSave();
    }


    var defaultOptions = {
        success: function () {
            if (window.parent) {
                window.parent.window.location.reload();
            } else {
                window.location.reload();
            }
        },
        error: function (response) {
            showError(response, options ? options.box : undefined);
        },
        message: function (response) {
            showMessage(response, options ? options.box : undefined);
        },
        timeout: function () {
            clearTimeout(timeoutId);
            $(".loading").hide();
        },
        validate: function () {
            return true;
        }

    };
    var settings = $.extend(defaultOptions, options);




    var element = $(this);

    if (element.is("a")) {
        action = action || element.attr("href");
        data = data || "";
    }
    else {
        action = action || element.attr("action");
        data = data || element.serialize();
    }

    var invalidHanlder = options ? options.invalid : undefined;

    if (element.mvcvalidate(data, invalidHanlder) == false) {
        return false;
    }

    if (settings.validate() == false) {
        return false;
    }

    var timeoutId = window.setTimeout(function () { $(".loading").show(); }, 150);

    var fn = function (response, status, xhr) {
        clearTimeout(timeoutId);
        $(".loading").hide();

        if (xhr.status == 278) {
            var win = window.parent || window;
            win.location.href = xhr.getResponseHeader('Location');
            return;
        }

        if (response === true) {
            settings.success(true);
        } else if (typeof response.Data != 'undefined') {
            settings.success(response.Data);
        } else {
            if (response.Error) {
                settings.error(response.Error);
            } else {
                settings.message(response.Message || response);
            }
        }

    };

    $.ajax({
        type: 'POST',
        url: action,
        data: data,
        success: fn,
        timeout: settings.timeout,
        dataType: "json"
    });
};

function showMessage(data,el) {
    var message = _formatMessage(data);


    var box;

    if (typeof el != "undefined") {
        box = el.find(".message-box.error:first");
    } else {
        box = $("#message-box-info");
    }

    if (box.size() > 0) {

        box.removeClass("hide").find("p").html(message);

        setTimeout(function () { box.addClass("hide"); }, 5000);
    } else {
        alert(message);
    }
}

function showError(data,el) {
    var message = _formatMessage(data);

    var box;

    if (typeof el != "undefined") {
        box = el.find(".message-box.info:first");
    } else {
        box = $("#message-box-info");
    }

    
    if (box.size() > 0) {
        box.removeClass("hide").find("p").html(message);
        setTimeout(function () { box.addClass("hide"); }, 5000);
    } else {
        alert(message);
    }
}

function _formatMessage(data) {

    if (data instanceof Array) {
        return data.join("<br/>");
    } else {
        return data ? data.toString() : "";
    }
}


